home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / common / SplitImage2.pyo (.txt) < prev    next >
Python Compiled Bytecode  |  2008-10-13  |  12KB  |  350 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import wx
  5. from util import to_storage, print_timing, Storage, do
  6. from gui.toolbox import get_wxColor
  7. from gui import skin
  8.  
  9. class SplitImage2(object):
  10.     
  11.     def __init__(self, image_dict):
  12.         imgs = self.image_dictionary = to_storage(image_dict)
  13.         image = skin.load_image(imgs.source)
  14.         self.cache = Storage()
  15.         clipcolor = imgs.get('clipcolor', None)
  16.         if clipcolor is not None:
  17.             self.cache.clipcolor = get_wxColor(imgs.clipcolor)
  18.             image.SetMask(wx.Mask(image, self.cache.clipcolor))
  19.         
  20.         imgw = image.GetWidth()
  21.         self.imgw = imgw
  22.         imgh = image.GetHeight()
  23.         self.imgh = imgh
  24.         self.draw_commands = []
  25.         if imgs.corners:
  26.             self.regions_to_draw = None
  27.             cornw = imgs.cornw = imgs.corners.size[0]
  28.             cornh = imgs.cornh = imgs.corners.size[1]
  29.             if imgs.corners.side == 'left':
  30.                 self.corners_to_draw = [
  31.                     'top_left',
  32.                     'bottom_left']
  33.                 if imgw - cornw > 0:
  34.                     self.cache.large_right = image.GetSubBitmap(wx.Rect(cornw, 0, imgw - cornw, imgh))
  35.                     self.regions_to_draw = [
  36.                         'left',
  37.                         'large_right']
  38.                 else:
  39.                     self.regions_to_draw = [
  40.                         'left']
  41.             elif imgs.corners.side == 'right':
  42.                 self.corners_to_draw = [
  43.                     'top_right',
  44.                     'bottom_right']
  45.                 if imgw - cornw > 0:
  46.                     self.cache.large_left = image.GetSubBitmap(wx.Rect(0, 0, imgw - cornw, imgh))
  47.                     self.regions_to_draw = [
  48.                         'large_left',
  49.                         'right']
  50.                 else:
  51.                     self.regions_to_draw = [
  52.                         'right']
  53.             elif imgs.corners.side == 'top':
  54.                 self.corners_to_draw = [
  55.                     'top_left',
  56.                     'top_right']
  57.                 if imgh - cornh > 0:
  58.                     self.cache.large_bottom = image.GetSubBitmap(wx.Rect(0, cornh, imgw, imgh - cornh))
  59.                     self.regions_to_draw = [
  60.                         'top',
  61.                         'large_bottom']
  62.                 else:
  63.                     self.regions_to_draw = [
  64.                         'top']
  65.             elif imgs.corners.side == 'bottom':
  66.                 self.corners_to_draw = [
  67.                     'bottom_left',
  68.                     'bottom_right']
  69.                 if imgh - cornh > 0:
  70.                     self.cache.large_top = image.GetSubBitmap(wx.Rect(0, 0, imgw, imgh - cornh))
  71.                     self.regions_to_draw = [
  72.                         'large_top',
  73.                         'bottom']
  74.                 else:
  75.                     self.regions_to_draw = [
  76.                         'bottom']
  77.             else:
  78.                 self.corners_to_draw = [
  79.                     'top_left',
  80.                     'top_right',
  81.                     'bottom_left',
  82.                     'bottom_right']
  83.                 self.regions_to_draw = [
  84.                     'left',
  85.                     'right',
  86.                     'top',
  87.                     'bottom',
  88.                     'center']
  89.             [ self.draw_commands.append((getattr(self, 'draw_' + corner + '_corner'), None)) for corner in self.corners_to_draw ]
  90.             if 'top_right' in self.corners_to_draw:
  91.                 self.cache.top_right = image.GetSubBitmap(wx.Rect(imgw - cornw, 0, cornw, cornh))
  92.             
  93.             if 'bottom_left' in self.corners_to_draw:
  94.                 self.cache.bottom_left = image.GetSubBitmap(wx.Rect(0, imgh - cornh, cornw, cornh))
  95.             
  96.             if 'bottom_right' in self.corners_to_draw:
  97.                 self.cache.bottom_right = image.GetSubBitmap(wx.Rect(imgw - cornw, imgh - cornh, cornw, cornh))
  98.             
  99.             if 'left' in self.regions_to_draw:
  100.                 self.cache.left = image.GetSubBitmap(wx.Rect(0, cornh, cornw, imgh - cornh * 2))
  101.             
  102.             if 'right' in self.regions_to_draw:
  103.                 self.cache.right = image.GetSubBitmap(wx.Rect(imgw - cornw, cornh, cornw, imgh - cornh * 2))
  104.             
  105.             if 'top' in self.regions_to_draw:
  106.                 self.cache.top = image.GetSubBitmap(wx.Rect(cornw, 0, imgw - cornw * 2, cornh))
  107.             
  108.             if 'bottom' in self.regions_to_draw:
  109.                 self.cache.bottom = image.GetSubBitmap(wx.Rect(cornw, imgh - cornh, imgw - cornw * 2, cornh))
  110.             
  111.             if 'center' in self.regions_to_draw:
  112.                 self.cache.center = image.GetSubBitmap(wx.Rect(cornw, cornh, imgw - cornw * 2, imgh - cornh * 2))
  113.             
  114.         else:
  115.             self.cache.total = image
  116.             self.cache['totalch'] = 'left'
  117.             self.cache['totalcv'] = 'top'
  118.             self.cache['totalco'] = [
  119.                 0,
  120.                 0]
  121.             self.regions_to_draw = [
  122.                 'total']
  123.         for region in self.regions_to_draw:
  124.             if region in self.image_dictionary.regions:
  125.                 region_dict = self.image_dictionary.regions[region]
  126.                 style = region_dict['style']
  127.                 self.draw_commands.append((getattr(self, 'draw_' + region), style))
  128.                 if style == 'static' or self.image_dictionary.style == 'static':
  129.                     color = get_wxColor(region_dict['color'])
  130.                     if color is not None:
  131.                         self.cache[region + 'cb'] = wx.Brush(color)
  132.                     else:
  133.                         self.cache[region + 'cb'] = None
  134.                     self.cache[region + 'ch'] = region_dict['halign']
  135.                     self.cache[region + 'cv'] = region_dict['valign']
  136.                     self.cache[region + 'co'] = region_dict['offset']
  137.                 
  138.             self.image_dictionary.style == 'static'
  139.             self.draw_commands.append((getattr(self, 'draw_' + region), self.image_dictionary['style']))
  140.         
  141.  
  142.     
  143.     def get_num_anchors_to(self):
  144.         return len(self.get_anchors_to())
  145.  
  146.     
  147.     def get_anchors_to(self):
  148.         return _[1]
  149.  
  150.     
  151.     def draw_total(self, type):
  152.         args = ('total', 0, 0, self.rect.width, self.rect.height)
  153.         getattr(self, 'draw_region_' + type)(*args)
  154.  
  155.     
  156.     def draw_top_left_corner(self, unused):
  157.         self.dc.DrawBitmap(self.cache.top_left, self.rect.x, self.rect.y, True)
  158.  
  159.     
  160.     def draw_top_right_corner(self, unused):
  161.         self.dc.DrawBitmap(self.cache.top_right, self.rect.x + self.rect.width - self.cache.top_right.GetWidth(), self.rect.y, True)
  162.  
  163.     
  164.     def draw_bottom_left_corner(self, unused):
  165.         self.dc.DrawBitmap(self.cache.bottom_left, self.rect.x, self.rect.y + self.rect.height - self.cache.bottom_left.GetHeight(), True)
  166.  
  167.     
  168.     def draw_bottom_right_corner(self, unused):
  169.         self.dc.DrawBitmap(self.cache.bottom_right, self.rect.x + self.rect.width - self.cache.bottom_right.GetWidth(), self.rect.y + self.rect.height - self.cache.bottom_right.GetHeight(), True)
  170.  
  171.     
  172.     def draw_left(self, type):
  173.         args = ('left', 0, self.image_dictionary.cornh, self.image_dictionary.cornw, self.rect.height - self.image_dictionary.cornh * 2)
  174.         getattr(self, 'draw_region_' + type)(*args)
  175.  
  176.     
  177.     def draw_right(self, type):
  178.         args = ('right', self.rect.width - self.image_dictionary.cornw, self.image_dictionary.cornh, self.image_dictionary.cornw, self.rect.height - self.image_dictionary.cornh * 2)
  179.         getattr(self, 'draw_region_' + type)(*args)
  180.  
  181.     
  182.     def draw_top(self, type):
  183.         args = ('top', self.image_dictionary.cornw, 0, self.rect.width - self.image_dictionary.cornw * 2, self.image_dictionary.cornh)
  184.         getattr(self, 'draw_region_' + type)(*args)
  185.  
  186.     
  187.     def draw_bottom(self, type):
  188.         args = ('bottom', self.image_dictionary.cornw, self.rect.height - self.image_dictionary.cornh, self.rect.width - self.image_dictionary.cornw * 2, self.image_dictionary.cornh)
  189.         getattr(self, 'draw_region_' + type)(*args)
  190.  
  191.     
  192.     def draw_center(self, type):
  193.         args = ('center', self.image_dictionary.cornw, self.image_dictionary.cornh, self.rect.width - self.image_dictionary.cornw * 2, self.rect.height - self.image_dictionary.cornh * 2)
  194.         getattr(self, 'draw_region_' + type)(*args)
  195.  
  196.     
  197.     def draw_large_left(self, type):
  198.         args = ('large_left', 0, 0, self.rect.width - self.image_dictionary.cornw, self.rect.height)
  199.         getattr(self, 'draw_region_' + type)(*args)
  200.  
  201.     
  202.     def draw_large_right(self, type):
  203.         args = ('large_right', self.image_dictionary.cornw, 0, self.rect.width - self.image_dictionary.cornw, self.rect.height)
  204.         getattr(self, 'draw_region_' + type)(*args)
  205.  
  206.     
  207.     def draw_large_top(self, type):
  208.         args = ('large_top', 0, 0, self.rect.width, self.rect.height - self.image_dictionary.cornh)
  209.         getattr(self, 'draw_region_' + type)(*args)
  210.  
  211.     
  212.     def draw_large_bottom(self, type):
  213.         args = ('large_bottom', 0, self.image_dictionary.cornh, self.rect.width, self.rect.height - self.image_dictionary.cornh)
  214.         getattr(self, 'draw_region_' + type)(*args)
  215.  
  216.     
  217.     def draw_region_stretch(self, img_string, dest_x, dest_y, dest_w, dest_h):
  218.         if dest_w > 0 and dest_h > 0:
  219.             myimg = self.cache[img_string]
  220.             if img_string + 's' not in self.cache and self.cache[img_string + 's'].GetWidth() != dest_w or self.cache[img_string + 's'].GetHeight() != dest_h:
  221.                 temp = myimg.ConvertToImage()
  222.                 temp.Rescale(dest_w, dest_h)
  223.                 self.cache[img_string + 's'] = wx.BitmapFromImage(temp)
  224.                 self.cache[img_string + 'sr'] = wx.RegionFromBitmap(self.cache[img_string + 's'])
  225.                 if not self.cache[img_string + 'sr'].IsEmpty():
  226.                     self.cache[img_string + 'sr'].Offset(self.rect.x + dest_x, self.rect.y + dest_y)
  227.                 
  228.             
  229.             self.dc.DrawBitmap(self.cache[img_string + 's'], self.rect.x + dest_x, self.rect.y + dest_y, True)
  230.             self.rs.append(self.cache[img_string + 'sr'])
  231.         
  232.  
  233.     
  234.     def draw_region_tile(self, img_string, dest_x, dest_y, dest_w, dest_h):
  235.         if dest_w > 0 and dest_h > 0:
  236.             if img_string + 't' not in self.cache:
  237.                 self.cache[img_string + 't'] = wx.Brush(wx.RED)
  238.                 self.cache[img_string + 't'].SetStipple(self.cache[img_string])
  239.                 if 'clipcolor' in self.image_dictionary:
  240.                     self.cache[img_string + 't'].SetStyle(wx.STIPPLE)
  241.                 
  242.             
  243.             r = wx.Region(self.rect.x + dest_x, self.rect.y + dest_y, dest_w, dest_h)
  244.             self.rs.append(r)
  245.             if 'clipcolor' in self.image_dictionary:
  246.                 bmp = wx.EmptyBitmap(dest_w, dest_h, 32)
  247.                 memdc = wx.MemoryDC()
  248.                 memdc.SelectObject(bmp)
  249.                 memdc.SetBrush(self.cache[img_string + 't'])
  250.                 memdc.DrawRectangle(0, 0, dest_w, dest_h)
  251.                 memdc.SelectObject(wx.NullBitmap)
  252.                 memdc.SetBrush(wx.NullBrush)
  253.                 bmp.SetMask(wx.Mask(bmp, self.cache.clipcolor))
  254.                 self.dc.DrawBitmap(bmp, self.rect.x + dest_x, self.rect.y + dest_y)
  255.             else:
  256.                 self.dc.SetBrush(self.cache[img_string + 't'])
  257.                 self.dc.SetPen(wx.TRANSPARENT_PEN)
  258.                 self.dc.DrawRectangle(self.rect.x + dest_x, self.rect.y + dest_y, dest_w, dest_h)
  259.                 self.dc.SetPen(wx.NullPen)
  260.                 self.dc.SetBrush(wx.NullBrush)
  261.         
  262.  
  263.     
  264.     def draw_region_static(self, img_string, dest_x, dest_y, dest_w, dest_h):
  265.         if dest_w > 0 and dest_h > 0:
  266.             myimg = self.cache[img_string]
  267.             halign = self.cache[img_string + 'ch']
  268.             valign = self.cache[img_string + 'cv']
  269.             offset = self.cache[img_string + 'co']
  270.             if halign == 'left':
  271.                 x = 0
  272.             elif halign == 'right':
  273.                 x = dest_w - myimg.GetWidth()
  274.             else:
  275.                 x = dest_w / 2 - myimg.GetWidth() / 2
  276.             x = x + dest_x + self.rect.x + offset[0]
  277.             if valign == 'top':
  278.                 y = 0
  279.             elif valign == 'bottom':
  280.                 y = dest_h - myimg.GetHeight()
  281.             else:
  282.                 y = dest_h / 2 - myimg.GetHeight() / 2
  283.             y = y + dest_y + self.rect.y + offset[1]
  284.             self.dc.SetClippingRegion(self.rect.x + dest_x, self.rect.y + dest_y, dest_w, dest_h)
  285.             if img_string + 'cb' in self.cache and self.cache[img_string + 'cb'] is not None:
  286.                 self.dc.SetBrush(self.cache[img_string + 'cb'])
  287.                 self.dc.SetPen(wx.TRANSPARENT_PEN)
  288.                 self.dc.DrawRectangle(self.rect.x + dest_x, self.rect.y + dest_y, dest_w, dest_h)
  289.                 self.dc.SetPen(wx.NullPen)
  290.                 self.dc.SetBrush(wx.NullBrush)
  291.             
  292.             self.dc.DrawBitmap(myimg, x, y, True)
  293.             if img_string + 'r' not in self.cache:
  294.                 new = (wx.RegionFromBitmap(myimg), x, y)
  295.                 self.cache[img_string + 'r'] = new
  296.                 if not new[0].IsEmpty():
  297.                     new[0].Offset(x, y)
  298.                 
  299.             else:
  300.                 (old, oldx, oldy) = self.cache[img_string + 'r']
  301.                 if not old.IsEmpty():
  302.                     old.Offset(x - oldx, y - oldy)
  303.                 
  304.                 self.cache[img_string + 'r'] = (old, x, y)
  305.             self.rs.append(self.cache[img_string + 'r'][0])
  306.             self.dc.DestroyClippingRegion()
  307.         
  308.  
  309.     
  310.     def draw(self, dc, rect):
  311.         self.dc = dc
  312.         self.rect = rect
  313.         self.rs = []
  314.         do((lambda .0: for func, args in .0:
  315. func(args))(self.draw_commands))
  316.         self.region = wx.Region(0, 0, 0, 0)
  317.         for region in self.rs:
  318.             if not region.IsEmpty():
  319.                 self.region.UnionRegion(region)
  320.                 continue
  321.         
  322.         self.dc = None
  323.         self.rect = None
  324.  
  325.  
  326. if __name__ == '__main__':
  327.     from skins import images, skins
  328.     app = wx.PySimpleApp()
  329.     image = Storage()
  330.     image['style'] = 'static'
  331.     image['source'] = 'skins/default/checkerboard9.png'
  332.     image['corners'] = { }
  333.     image['halign'] = 'left'
  334.     image['valign'] = 'top'
  335.     image['offset'] = [
  336.         50,
  337.         50]
  338.     image['regions'] = dict(center = dict(style = 'static', color = 'red', valign = 'center', halign = 'center', offset = [
  339.         50,
  340.         50]))
  341.     skins.res_path = 'res/'
  342.     destbitmap = images.get('skins/default/blue-flower.jpg')
  343.     temp_dc = wx.MemoryDC()
  344.     temp_dc.SelectObject(destbitmap)
  345.     splitimg = SplitImage2(image)
  346.     splitimg.draw(temp_dc, wx.Rect(50, 50, 200, 200))
  347.     temp_dc.SelectObject(wx.NullBitmap)
  348.     destbitmap.SaveFile('C:/workspace/Digsby/res/skins/default/output.png', wx.BITMAP_TYPE_PNG)
  349.  
  350.